package com.tqw.auth.service.impl;

import com.tqw.auth.dao.RemoteUserMapper;
import com.tqw.auth.dao.SignInClientMapper;
import com.tqw.auth.pojo.ClientSignIn;
import com.tqw.auth.service.SignInClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Van
 */
@Service
public class SignInClientServiceImpl implements SignInClientService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private SignInClientMapper signInClientMapper;

    @Autowired
    private RemoteUserMapper remoteUserMapper;

    @Override
    public Boolean addClient(ClientSignIn clientSignIn) {
        if (remoteUserMapper.selectUserByUsername(clientSignIn.getUsername())==null) {
            clientSignIn.setPassword(passwordEncoder.encode(clientSignIn.getPassword()));
            Date date = new Date();
            clientSignIn.setCreatedTime(date).setUpdatedTime(date);
            signInClientMapper.addClient(clientSignIn);
            signInClientMapper.updateClient(clientSignIn);
            return true;
        }
        return false;
    }
}
